package com.example.jetpackcomposebasic.todo

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import kotlin.random.Random

/*@Preview
@Composable
private fun ListBody() {
    ToDoListBody(TestItems.examples())
}*/

@Composable
fun ToDoListBody(
    examples: List<ToDoItem>,
    addToDoItem: (ToDoItem) -> Unit,
    removeItem: (ToDoItem) -> Unit
) {


    ConstraintLayout(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
    ) {

        val (body, button) = createRefs()

        LazyColumn(
            Modifier
                .constrainAs(body) {
                    top.linkTo(parent.top)
                    start.linkTo(parent.start)
                    end.linkTo(parent.end)
                }
                .padding(horizontal = 8.dp)) {
            items(examples) {
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text(text = it.title)
                    IconButton(onClick = {
                        removeItem(it)
                    }) {

                        // 具有内部状态的可组合项 往往更难测试，不易于重复使用
                        val alpha = remember(it.id) {
                            // 一把锁对一把钥匙
                            randomAlpha()
                        }
                        Icon(

                            imageVector = it.icon,
                            contentDescription = null,
                            tint = Color.Green.copy(alpha = alpha)
                        )
                    }
                }
            }
        }

        Button(onClick = {
            addToDoItem(
                TestItems.randomItem()
            )
        }, Modifier.constrainAs(button) {
            top.linkTo(body.bottom)
            bottom.linkTo(parent.bottom)
            centerHorizontallyTo(parent)
            height = Dimension.wrapContent
        }) {
            Text(text = "添加一个吧")
        }
    }
}

fun randomAlpha() = Random.nextFloat().coerceIn(0.3f, 0.9f)